import { eOption, getCabinCode } from "./rule";
import { formatDate } from "@/utils/util";
export default {
  data() {
    return {
      chartShow: false,
      isEdit: false, //是否可以编辑预售
      Request: {
        date: "",
        airlineCode: "",
        cabinCode: "",
        from: "",
        to: ""
      },

      flightNoList: [
        {
          value: "CZ3000",
          label: "CZ3000"
        }
      ],
      cabinCodeList: [],
      show: false,
      cabinCodeResult: [],
      cabinRankResult: {},
      cabinCode: []
    };
  },
  computed: {},
  watch: {},
  methods: {
    search() {
      const params = {
        date: formatDate(this.Request.date),
        airlineCode: this.Request.airlineCode,
        from: this.Request.from,
        to: this.Request.to,
        cabinCode: this.Request.cabinCode
      };
      this.$http
        .post("/api/optimization", params)
        .then(res => {
          if (res.data.code == "601") {
            this.show = true;
            this.cabinCode = getCabinCode(res.data.data.list[0]);
            this.cabinCodeResult = res.data.data.list[0];
            this.cabinRankResult = res.data.data.list[1];
            return;
          }
          this.show = false;
          this.$Notice.error({
            title: "查询失败",
            desc: res.data.message
          });
        })
        .catch(err => {
          //请求失败的处理
          this.show = false;
          this.$Notice.error({
            title: "查询失败",
            desc: err
          });
          return false;
        });
    },
    adjust() {
      //this.isEdit = true;
    },
    apply() {},
    //显示图表
    async chart() {
      this.chartShow = true;
      let res = await this.getChartData();
      if (res) {
        this.drawChart(res);
      }
    },
    //绘制图表
    drawChart(data) {
      const bins = this.$ecStat.histogram(data, "sturges");
      const option = eOption(bins);
      const chart = this.$refs.chart;
      const myChart = this.$echarts.init(chart);
      myChart.setOption(option);
    },
    //获取图表数据
    getChartData() {
      let params = {
        flightNo: this.Request.airlineCode,
        depDate: formatDate(this.Request.date),
        departure: this.Request.from,
        arrive: this.Request.to
      };
      return this.$http
        .post("/api/statistics", params)
        .then(res => {
          if (res.data.status == "601") {
            return res.data.revenue;
          }
          this.$Notice.error({
            title: "查询失败",
            desc: res.data.message
          });
          return false;
        })
        .catch(err => {
          //请求失败的处理
          this.$Notice.error({
            title: "查询失败",
            desc: err
          });
          return false;
        });
    },
    isMainCabin(cabinCode) {
      let res = this.cabinCode.some(item => {
        return item == cabinCode;
      });
      return res;
    },
    //时间选择变化
    startDateChange(val) {
      if (!this.Request.airlineCode) return;
      let params = { flightNo: this.Request.airlineCode, date: val };
      this.getCabinList(params);
    },
    //航班变化
    airlineCodeChange(val) {
      if (!this.Request.date) return;
      let params = { flightNo: val, date: formatDate(this.Request.date) };
      this.getCabinList(params);
    },
    //请求舱位数据
    getCabinList(params) {
      params?params:{
        flightNo:this.Request.airlineCode,
        date:formatDate(this.Request.date)
      }
      this.cabinCodeList = [];
      this.$http.post("/api/cabin/list", params).then(res => {
        if (res.data.code == "601") {
          for (let i of res.data.data) {
            let cabin = {
              value: i.classCode,
              label: i.classCode
            };
            this.cabinCodeList.push(cabin);
          }
        }
      });
    },
    getSaleDataLastUpdateDate(){
      this.$http.post('/api/config/list')
        .then(res => {
            if(res.data.code=='601'){
              this.Request.date = res.data.data[0].saleDataLastUpdateDate;
            }
        })
    }
  },
  components: {},
  created() {
    let query = this.$route.query;
    if (query.date) {
      this.Request = JSON.parse(JSON.stringify(query));
      this.getCabinList();
      this.search();
    }else{
      this.getSaleDataLastUpdateDate();
    }
  }
};
